package com.tanhua.autoconfig;

import com.tanhua.autoconfig.properties.*;
import com.tanhua.autoconfig.template.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

//使smsProperties生效 或者在配置类上加上@Configuration注解
@EnableConfigurationProperties({
        SmsProperties.class,
        AipProperties.class,
        OssProperties.class,
        HuanXinProperties.class,
        GreenProperties.class
})


/**
 * 自动装配Template配置类，会自动执行@Bean注解，并将对象放入IOC容器
 */
public class TanhuaAutoConfiguration {

    @Bean
    public SmsTemplate smsTemplate(SmsProperties smsProperties){
        return new SmsTemplate(smsProperties);
    }

    @Bean
    public AipTemplate aipTemplate(){
        return new AipTemplate();
    }

    @Bean
    public OssTemplate ossTemplate(OssProperties ossProperties){
        return new OssTemplate(ossProperties);
    }

    @Bean
    public HuanXinTemplate huanXinTemplate(HuanXinProperties huanXinProperties){
        return new HuanXinTemplate(huanXinProperties);
    }

    /**
     * 检查配置文件中，是否具有tanhua.green开头的配置
     *      同时，其中的enable属性为true，才装配这个对象
     * @param properties
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "tanhua.green",value = "enable", havingValue = "true")
    public AliyunGreenTemplate aliyunGreenTemplate(GreenProperties properties) {
        return new AliyunGreenTemplate(properties);
    }
}


